#!/usr/bin/python

import unittest
import percolation_threshold

class PercolationThresholdTest(unittest.TestCase):

  def setUp(self):
    self.p_obj = percolation_threshold.PercolationThreshold(5)

  def testInit(self):
    self.assertEquals(27, self.p_obj.size)
    self.assertEquals(5, self.p_obj.x)
    self.assertEquals(
      [0, 1, 2, 3, 4,
       5, 6, 7, 8, 9,
       10, 11, 12, 13, 14,
       15, 16, 17, 18, 19,
       20, 21, 22, 23, 24,
       25, 26], self.p_obj.id_list)
    self.assertEquals(0, self.p_obj.num_open)
    self.assertEquals(25, self.p_obj.upper_virtual)
    self.assertEquals(26, self.p_obj.lower_virtual)
    self.assertTrue(self.p_obj.id_status[self.p_obj.upper_virtual])
    self.assertTrue(self.p_obj.id_status[self.p_obj.lower_virtual])

  def testthreshold(self):
    p_obj = percolation_threshold.PercolationThreshold(5)
    self.assertAlmostEqual(0, p_obj.threshold)
    p_obj.num_open = 4
    self.assertAlmostEqual(0.16, p_obj.threshold)
 
  def testGetNeighbors(self):
    self.assertEquals([], self.p_obj.GetNeighbors(-1))
    self.assertEquals([1, 5, 25], self.p_obj.GetNeighbors(0))
    self.assertEquals([1, 3, 7, 25], self.p_obj.GetNeighbors(2))
    self.assertEquals([4, 8, 14], self.p_obj.GetNeighbors(9))
    self.assertEquals([10, 16, 20], self.p_obj.GetNeighbors(15))
    self.assertEquals([12, 16, 18, 22], self.p_obj.GetNeighbors(17))
    self.assertEquals([14, 18, 24], self.p_obj.GetNeighbors(19))
    self.assertEquals([15, 21, 26], self.p_obj.GetNeighbors(20))
    self.assertEquals([17, 21, 23, 26], self.p_obj.GetNeighbors(22))
    self.assertEquals([19, 23, 26], self.p_obj.GetNeighbors(24))
    self.assertEquals([], self.p_obj.GetNeighbors(25))
    self.assertEquals([], self.p_obj.GetNeighbors(26))
    self.assertEquals([], self.p_obj.GetNeighbors(27))

if __name__ == '__main__':
  unittest.main()
